﻿using MediatR;
using S.Parturition.API.Application.Command;
using S.Parturition.Domain;

namespace S.Parturition.API.Application.CommandHandlers
{
    public class UpdateParturitionHandler : IRequestHandler<UpdateParturitionCommand, bool>
    {

        private readonly MyDbContext dbContext;

        public UpdateParturitionHandler(MyDbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public async Task<bool> Handle(UpdateParturitionCommand request, CancellationToken cancellationToken)
        {
            //获取到对应耳号的信息
            var parturition = dbContext.ParturitionModel.Where(x => x.EarTag == request.EarTag).OrderByDescending(x => x.CreateDate).FirstOrDefault();

            parturition.SheepStatus = parturition.SheepStatus == true ? false : true; // 修改羊只状态

            //修改
            dbContext.Update(parturition);
            return dbContext.SaveChanges() > 0;
        }
    }
}
